package com.jiyun.auth.service;

import com.jiyun.auth.feign.UserClient;
import com.jiyun.auth.properties.JwtProperties;
import com.jiyun.leyou.pojo.UserInfo;
import com.jiyun.leyou.util.JwtUtils;
import com.jiyun.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthService {

    @Autowired
    UserClient userClient;
    @Autowired
    JwtProperties jwtProperties;

    // fegin不能传实体类
    public String valid(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User u = userClient.queryUser(username,password);
        if(u==null){
            return null;
        }else{

            try {
                // ras加密，JWT生成
                UserInfo userInfo = new UserInfo();
                userInfo.setId(u.getId());
                userInfo.setUsername(u.getUsername());
                String token = JwtUtils.generateToken(userInfo, jwtProperties.getPrivateKey(), jwtProperties.getExpire());

                return token;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
